package com.hspedu.collection_;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author DL5O
 * @version 1.0
 */
@SuppressWarnings("all")
public class CollectionIterator01 {
    public static void main(String[] args) {
        Collection col = new ArrayList();

        col.add(new Book("三国演义", "罗贯中", 10.1));
        col.add(new Book("小李飞刀", "古龙", 5.1));
        col.add(new Book("红楼梦", "曹雪芹", 34.6));
        col.add("jack");

//        System.out.println("col" + col);


        //现在想一本一本的遍历这个 col 集合
        //1.先得到这个集合对应的迭代器
        Iterator iterator = col.iterator();
        //2.使用while循环遍历
        /*while (iterator.hasNext()) {//判断是否还有数据
            //next方法返回的数据，是下一个元素，类型是Object，
            //编译类型 Object
            //运行类型 取决于真正存放的类型
            Object obj = iterator.next();
            System.out.println("obj=" + obj);
        }*/

        //快捷键，快速生成 ==> itit
        //快捷键提示 ctrl + j
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println("obj=" + next);
        }
        //3.当退出while循环后，这时iterator迭代器，指向的是最后的元素
        //iterator.next();//报异常：NoSuchElementException

        //4.如果需要再次遍历，需要重置迭代器
        System.out.println("====第二次====");
        iterator = col.iterator();
        while(iterator.hasNext()){
            Object next = iterator.next();
            System.out.println("obj=" + next);
        }
    }
}

@SuppressWarnings("all")
class Book {
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}